//
//  CZStatusRetweetView.swift
//  weibo
//
//  Created by Apple on 16/3/16.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import SnapKit
class CZStatusRetweetView: UIView {
    //  记录当前视图的底部约束
    var bottomConstraint: Constraint?
    var statusViewModel: CZStatusViewModel? {
        didSet {
            //  通过viewmodel封装转发内容
            //contentLabel.text = statusViewModel?.retweetContent
            contentLabel.attributedText = statusViewModel?.retweetAttributeStr
            //  设置匹配结果
            contentLabel.matchResults = statusViewModel?.retweetMatchResults
            //  根据转发微博的图片数量是否大于0判断是否显示图片配图，如果数量大于0 显示配图，更新约束，设置数据
            bottomConstraint?.uninstall()
            //  更新约束
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count > 0 {
                pictureView.hidden = false
                snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(StatusCellMargin).constraint
                })
                
                //  设置数据
                pictureView.picUrls = picUrls
                
            } else {
                pictureView.hidden = true
                snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(StatusCellMargin).constraint
                })
            }
            
            
        }
    
    }

    // MARK:    懒加载控件
    private lazy var contentLabel: CZStatusLabel = {
        let label = CZStatusLabel(textColor: UIColor.darkGrayColor(), fontSize: contentLabelFontSize)
        label.numberOfLines = 0
        return label
    }()
    
    //  配图
    private lazy var pictureView: CZStatusPictureView = {
        let collectionView = CZStatusPictureView()
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //  灰色
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        //  添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //  设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusCellMargin)
            make.leading.equalTo(self).offset(StatusCellMargin)
            make.trailing.equalTo(self).offset(-StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.leading.equalTo(contentLabel)
            //make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView).offset(StatusCellMargin).constraint
        }
        
        //contentLabel.text = "哈哈啊啊哈哈市的开发商极度疯狂哈哈啊啊哈哈市的开发商极度疯狂哈哈啊啊哈哈市的开发商极度疯狂哈哈啊啊哈哈市的开发商极度疯狂"
        
    }
    
    
    
}
